# 创建时间:2023/3/16 21:39
# 创建人:天霄
# 计划 :SenjoTwintail
# 文件 :UiApi.py
# 基于 Python 3.8.2
# ========================================
# 根据plist直接返回图片及裁剪数据
# ========================================
from vars import CONST
import plistlib


# 登录页标志
# F:/a_python_file/my/SenjoTwintail/Data/assets/res/ui/gameshare/res
# 首次选军士
# F:/a_python_file/my/SenjoTwintail/Data/assets/res/ui/novicetask/res/


# 抽卡界面
# F:/战场双马尾/资料/com.kurogame.oppai.bili/assets/res/ui/gachabeganeffect/res
# 公共界面按钮、图标
# F:/战场双马尾/资料/com.kurogame.oppai.bili/assets/res/ui/common/res
# 模块按钮
# F:/战场双马尾/资料/com.kurogame.oppai.bili/assets/res/ui/equipment/res

# ========宿舍
# 按钮、信息栏
# F:/战场双马尾/资料/com.kurogame.oppai.bili/assets/res/ui/favor/res
# F:/战场双马尾/资料/com.kurogame.oppai.bili/assets/res/ui/favordetail/res

# /res/ui/common/res  主界面公共按钮状态等一系列

class UiApi:
    """UI相关文件路径"""
    pictures = {  # *号代表后续需要支持可配置（可能不在这里获取）
        'icon': r'icon/icon.png',  # 程序图标
        'loading': 'res/ui/loading/res/loadinglayer.png',  # 加载中
        'touchguide': 'res/guide/touchguide/res/touchguide.png',  # 触摸向导（即鼠标）

        'loginbg': 'res/ui/login/res/bg2.png',  # *****登录页-背景图
        'loginlayer': 'res/ui/login/res/loginlayer.png',  # 登录-界面布局

        'resourcebg2': 'res/ui/common/res/resourcebg2.png',  # 公共-状态栏背景

        'mainbg': 'res/bg/storybg/leader01.jpg',  # ******主页-背景图
        'mainuilayer': 'res/ui/mainui/res/mainuilayer.png',  # 主页-menu按钮和宣传图
        'mainuilayer1': 'res/ui/mainui/res/mainuilayer1.png',  # 主页-状态栏
        'playerexp': 'res/ui/mainui/res/exp.png',  # 主页-主角exp
        'mainmenuitem': 'res/ui/mainui/res/mainmenuitem.png',  # 主页-按钮
        'mainmenu_bg3': 'res/ui/mainui/res/bg3.png',  # 主页-底部栏

        'countdownbg': 'res/ui/questfavoreventitem/res/countdownbg.png',  # 一个状态的背景管（无plist）
        'battlecontroluilayer2': 'res/ui/battlecontrolui/res/battlecontroluilayer2.png',  # 含单独的小钱钱状态
        'rankinfo': 'res/ui/rankinfo/res/rankinfo.png',  # 等级信息，含数字
        'topcurrencylayer': 'res/ui/topcurrencylayer/res/topcurrencylayer.png',  # 紫砖、状态管、加减号
        'actionpointpg': 'res/ui/topcurrencylayer/res/actionpointpg.png',  # 体力条

        'commonlayer': 'res/ui/common/res/commonlayer.png',  # 公共各种按钮、ui
        'commonbutton': 'res/ui/common/res/button.png',  # 公共几乎所有按钮
        'commontop': 'res/ui/common/res/top.png',  # 公共顶部栏

        'directoryBg': 'res/bg/activityinstancebg/actinstance22.jpg',  # ******宿舍-背景图
        'favorlistlayer': 'res/ui/favorlist/res/favorlistlayer.png',  # 宿舍-侧边栏背景
        'favorlistbg': 'res/ui/favorlist/res/bg3.png',  # 宿舍-侧边栏背景
        'favorlistbg4': 'res/ui/favorlist/res/bg4.png',  # 宿舍-侧边栏背景
        'favorlistitemlayer': 'res/ui/favorlistitem/res/favorlistitemlayer.png',  # 宿舍-侧边栏列表
        'favorlayer': 'res/ui/favor/res/favorlayer.png',  # 宿舍-主界面
        'favorlayer2': 'res/ui/favor/res/favorlayer2.png',  # 宿舍-各种按钮相关
        'favordetaillayer': 'res/ui/favordetail/res/favordetaillayer.png',  # 宿舍-主界面详细（姓名栏、底部按钮等）
        'favoreventlistitemlayer': 'res/ui/favoreventlistitem/res/favoreventlistitemlayer.png',  # 宿舍-主界面详细（空白栏）
        'thermometer': 'res/ui/favor/res/thermometer.png',  # 宿舍-好感度
        'clothbg': 'res/ui/favor/res/clothbg.png',  # 宿舍-换装背景阴影
        'reviewbglayer': 'res/ui/review/res/reviewbglayer.png',  # 宿舍-换装时的窗帘、横杆等
        'talklistitembg': 'res/ui/favor/res/talklistitembg.png',  # 宿舍-CV语音背景阴影

        'favorRecordBg2': 'res/ui/favor/res/back2.png',  # 宿舍-角色文字记录背景6阴影
        'favorRecordBg1': 'res/ui/favor/res/back1.png',  # 宿舍-角色文字记录背景纯阴影

        'baglayer': 'res/ui/bag/res/baglayer.png',  # 背包-按钮
    }

    effect = {
        'favorscoreupeffect': 'res/effect/favorscoreupeffect/res/favorscoreupeffect.png',  # 好感值
        'favorupeffect': 'res/effect/favorupeffect/res/favorupeffect.png',  # 好感升级

    }

    fonts = {
        'font1': 'res/font/font1.ttf',  # 较圆润的字体，支持汉字不太多
        'ERASDEMI': 'res/font/ERASDEMI.TTF',  # 仅英文半角字符
        'MFLiHei': 'res/font/MFLiHei_Noncommercial-Regular.ttf',  # 适用于大标题，粗宽字体
        'MFShangHei': 'res/font/MFShangHei_Noncommercial-Regular.ttf',  # 默认，大段文字使用的字体
    }

    sounds = {
        'favorTouchBegan': 'res/sound/cn/ui/favortouchbegansound.mp3',  # 触摸军士的声音1
        'favorTouch': 'res/sound/cn/ui/favortouchsound.mp3',  # 触摸军士的声音2

        'buttonNone': 'res/sound/cn/common/buttonsound.mp3',  # 点击空白处的声音
        'buttonNormal': 'res/sound/cn/common/vc001.mp3',  # 点击一般按钮的声音
        'buttonPage': 'res/sound/cn/common/vc002.mp3',  # 点击翻页的声音
        'buttonLock': 'res/sound/cn/common/vc010.mp3',  # 点击禁用按钮的声音
        'changeCloth': 'res/sound/cn/common/vc012.mp3',  # 换装的声音
        'buttonReturn': 'res/sound/cn/common/vc017.mp3',  # 点击返回的声音
        'buttonStart': 'res/sound/cn/common/vc022.mp3',  # 点击开始的声音（“布铃”）

        'loginBg': 'res/sound/cn/common/battlebgsound3.mp3',  # 登录页背景音乐
        'mainBg': 'res/sound/cn/common/mainbgsound.mp3',  # 主页背景音乐
        'favorBg': 'res/sound/cn/common/favorbgsound.mp3',  # 宿舍背景音乐
    }

    def getUiPathPlist(self, where):
        """获取图片路径、plist路径"""
        pic_path = CONST.PATH.ASSETS_ROOT / self.pictures[where]
        plist_path = pic_path.parent / (pic_path.stem + '.plist')
        return pic_path, plist_path

    def getUiPicture(self, where, which=None):
        """获取图片和裁剪尺寸

        :param where: 哪张图片，据此获得图片路径
        :param which: 图片的哪个部分，据此获得裁剪尺寸
        :return: pic_path, size
        """
        pic_path = CONST.PATH.ASSETS_ROOT / self.pictures[where]
        if not which:
            return pic_path, None
        plist_path = pic_path.parent / (pic_path.stem + '.plist')
        with open(plist_path, 'rb') as f:
            plist_dict = plistlib.load(f)
        to_list = lambda x: tuple(map(int, x.replace('{', '').replace('}', '').split(',')))
        size: str = plist_dict['frames'][which]['frame']
        size: tuple = to_list(size)
        return pic_path, size

    def getEffectPathPlist(self, where):
        """获取特效图片路径、plist路径"""
        pic_path = CONST.PATH.ASSETS_ROOT / self.effect[where]
        plist_path = pic_path.parent / (pic_path.stem + '.plist')
        return pic_path, plist_path

    def getPathFont(self, where):
        """获取字体文件"""
        return CONST.PATH.ASSETS_ROOT / self.fonts[where]

    def getPathSound(self, where):
        """获取音频文件路径"""
        return CONST.PATH.ASSETS_ROOT / self.sounds[where]






